package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.mybatis.spring.annotation.MapperScan;
import org.py.mymodule.submodule.entity.Commodity;
import org.py.mymodule.submodule.entity.Commodityform;
import org.py.mymodule.submodule.common.Result;
import org.py.mymodule.submodule.mapper.CommodityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/commodity")
@MapperScan("org.py.mymodule.submodule.mapper")
public class CommodityController {

    @Autowired
    private CommodityMapper commodityMapper;

    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }

    // 商品的添加
    @PostMapping
    public String addCommodity(@RequestBody Commodityform commodityform) throws Exception {
        // 创建一个新的Commodity对象
        Commodity commodity = new Commodity();
        commodity.setCommodityName(commodityform.getCommodityName());
        commodity.setCommodityPrice(commodityform.getCommodityPrice());
        commodity.setCommodityCategory(commodityform.getCommodityCategory());
        commodity.setCommodityInventory(commodityform.getCommodityInventory());
        commodity.setCommodityDescribe(commodityform.getCommodityDescribe());
        // 这里应该有逻辑来处理添加商品的请求
        Commodity existingCommodity = commodityMapper.selectById(commodity.getCommodityId());
        if (existingCommodity != null) {

            return "Commodity with id " + commodity.getCommodityId() + " already exists";
        }//当id已存在时，返回错误
        commodityMapper.insert(commodity);
        return "Commodity added successfully";
    }

    // 按ID查询，按ID
    @GetMapping("/getid/{id}")
    public List<Commodity> getCommodityListById(@PathVariable("id") Integer id) {
        QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("commodity_id", id);
        List<Commodity> commodities = commodityMapper.selectList(queryWrapper);
        return commodities;
    }

    //商品的模糊查询
    @GetMapping("/search")
    public List<Commodity> searchCommodity(@RequestParam String keyword){
        QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("commodity_name",keyword);
        List<Commodity> commodities = commodityMapper.selectList(queryWrapper);
        return commodities;
    }


    // 查询所有
    @GetMapping("/getall")
    public List<Commodity> getCommodityList() {
        QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
        return commodityMapper.selectList(queryWrapper);
    }

    //  按类查询，按类
    @GetMapping("/gettype")
    public List<Commodity> gettypeCommodityList(@RequestParam String type) {
        QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("commodity_category", type);
        List<Commodity> commodities = commodityMapper.selectList(queryWrapper);
        return commodities;
    }

    // 商品的删除，按id删除
    @DeleteMapping("/delete/{id}")
    public String deleteCommodity(@PathVariable("id") Long id) {
        int result = commodityMapper.deleteById(id);
        if (result > 0) {
            return "Commodity with ID " + id + " deleted successfully";
        } else {
            return "Commodity with ID " + id + " not found";
        }
    }

    //商品的删除，按类删除
    @DeleteMapping("/deletetype")
    public String deletetypeCommodity(@RequestParam String type) {
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("commodity_category", type);
        int rows = commodityMapper.deleteByMap(columnMap);
        if (rows > 0) {
            System.out.println("Commodity deleted successfully");
            return "Commodity deleted successfully.";
        } else {
            return "No users deleted.";
        }

    }


    // 商品的更新
    // 商品的更新
    @PutMapping("/update")
    public Result updateCommodity(@RequestBody Commodity commodity) {
        //这里应该有逻辑来处理更新商品的请求
        QueryWrapper<Commodity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("commodity_id", commodity.getCommodityId());
        int result = commodityMapper.update(commodity, queryWrapper);
        ;
        if (result > 0) {
            return Result.success();
        } else {
            return Result.error();
        }
    }
}
